﻿namespace MSSqlSchemaDoc.Xslt
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Contains the information of an Xml schema or documentation file.
    /// </summary>
    public class XmlFileInfo : IComparable<XmlFileInfo>
    {
        /// <summary>
        /// The order in which to sort different object types.
        /// </summary>
        private static IDictionary<string, int> sortTypeOrder;

        /// <summary>
        /// Initializes a new instance of the XmlFileInfo class.
        /// </summary>
        public XmlFileInfo()
        {
            if (sortTypeOrder == null)
            {
                sortTypeOrder = new Dictionary<string, int>();
                sortTypeOrder.Add("Table", 10);
                sortTypeOrder.Add("View", 20);
                sortTypeOrder.Add("StoredProcedure", 30);
                sortTypeOrder.Add("UserDefinedFunction", 40);
                sortTypeOrder.Add("Synonym", 50);
                sortTypeOrder.Add("FullTextCatalog", 60);
                sortTypeOrder.Add("DdlTrigger", 70);
                sortTypeOrder.Add("DmlTrigger", 80);
            }
        }

        /// <summary>
        /// Gets or sets the name of the document element.
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// Gets or sets the name of the object.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the schema of the object.
        /// </summary>
        public string Schema { get; set; }

        /// <summary>
        /// Gets or sets the full path with file name and extension.
        /// </summary>
        public string FullPath { get; set; }

        /// <summary>
        /// Gets or sets the key for the object's related documentation file.
        /// </summary>
        public string DocumentationFileKey { get; set; }

        /// <summary>
        /// Gets or sets the full path to the result file.
        /// </summary>
        public string ResultFileFullPath { get; set; }

        /// <summary>
        /// Gets or sets the filename without path information of the results file.
        /// </summary>
        public string ResultFileName { get; set; }

        #region IComparable<XmlFileInfo> Members

        /// <summary>
        /// Compares against another XmlFileInfo object.
        /// </summary>
        /// <param name="other">The object to compare to.</param>
        /// <returns>The comparison value.</returns>
        public int CompareTo(XmlFileInfo other)
        {
            int order = GetSortTypeOrder(this.Type);
            int orderOther = GetSortTypeOrder(other.Type);

            if (order != orderOther)
            {
                return order - orderOther;
            }
            else if (this.Name.Equals(other.Name))
            {
                if (string.IsNullOrEmpty(this.Schema))
                {
                    return 1;
                }
                else
                {
                    return this.Schema.CompareTo(other.Schema);
                }
            }
            else
            {
                return this.Name.CompareTo(other.Name);
            }
        }

        #endregion

        /// <summary>
        /// Gets the sort value for an object type.
        /// </summary>
        /// <param name="type">The type of object.</param>
        /// <returns>The sort value.</returns>
        private static int GetSortTypeOrder(string type)
        {
            if (sortTypeOrder.ContainsKey(type))
            {
                return sortTypeOrder[type];
            }
            else
            {
                return 9999;
            }
        }
    }
}
